import 'package:flutter/material.dart';
import '../widges/punch_card_widget.dart'; 
import '../widges/banner_carousel.dart';
import '../widges/ddl_list.dart';
import '../widges/course_list.dart';
import '../widges/course_card.dart';
import '../app_data_manager.dart';
import '../models/ddl_item.dart';

/// 主页组件
class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  /// 只会在页面初始化时调用一次
  @override
  void initState() {
    super.initState();
    // 确保数据加载后刷新页面
    AppDataManager().init().then((_) {
      /// setState() 会刷新整个页面组件（将数据更新到UI上）
      setState(() {
        /// 这里可以添加页面加载后的一些操作
      });
    });
  }

  /// 每次页面更新时都会调用，构建页面的组件
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      endDrawerEnableOpenDragGesture: false,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        automaticallyImplyLeading: false,
      ),
      endDrawer: Align(
        alignment: Alignment.centerRight,
        child: Container(
          width: MediaQuery.of(context).size.width * 0.5,
          height: MediaQuery.of(context).size.height - kBottomNavigationBarHeight,
          color: Colors.white,
          child: Column(
            children: const [
              SizedBox(height: 40),
              Text('副导航栏', style: TextStyle(fontSize: 20)),
            ],
          ),
        ),
      ),
      body: const HomeContent(),/// 主页内容组件
    );
  }
}

// 主页内容组件
class HomeContent extends StatefulWidget {
  const HomeContent({super.key});

  @override
  State<HomeContent> createState() => _HomeContentState();
}

class _HomeContentState extends State<HomeContent> {
  @override
  void initState() {
    super.initState();
    // 确保数据加载后刷新页面
    AppDataManager().init().then((_) {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    final List<CourseInfo> todayCourses = AppDataManager().getTodayCourses()
        .map((course) => CourseInfo.fromJson(course))
        .toList();

    // 主页DDL数据：未完成且未回收，按截止时间升序排序
    final List<DDLItem> sortedDdls = List.from(AppDataManager().getDDLList())
      ..sort((a, b) => (a.deadline ?? DateTime(2100)).compareTo(b.deadline ?? DateTime(2100)));
    
    /// 主页滚动容器组件
    return SingleChildScrollView(
      child: Column(
        children: [
          const Divider(height: 1, thickness: 1),
          const PunchCardWidget(),
          BannerCarousel(
            banners: const [
              Icon(Icons.image, size: 60, color: Colors.blue),
              Icon(Icons.image, size: 60, color: Colors.green),
              Icon(Icons.image, size: 60, color: Colors.orange),
            ],
          ),
          CourseList(courses: todayCourses), /// 课程列表组件
          DDLList(ddls: sortedDdls), /// DDL列表组件
        ],
      ),
    );
  }
} 